package communicationMessages;

import java.util.logging.Level;
import database.DB;
import logger.LoggerWriter;

public class NumOfMsgsMessage extends Message { //return the total msgs in subforum

	@Override
	public String getAnswer() {
		String []arr = this.content.split(delimiter);
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		if (arr.length != 2)
		{
			logger.log("failed"+"NumOfMsgsMessage wrong format"+delimiter, Level.WARNING);
			return "failed"+delimiter+"NumOfMsgsMessage Message wrong format"+delimiter;
		}
		String answer = "";
		String userName = arr[0];
		String subForumId = arr[1];
		String userRoll = DB.getUserRoll(userName);
		boolean isAdmin = DB.isAdmin(userName);
		boolean subForumExist = DB.isSubForumExist(subForumId);
		if (!isAdmin)
		{	logger.log("not an admin try get num of msgs", Level.WARNING);
			return "failed" + delimiter + "not an admin try get num of msgs";
			}
		else{
			if (subForumExist){
				int numberOfmsgs =  DB.getNumberOfMessagesInSubForum(subForumId);
				answer = "numOfMsgs" + delimiter + userRoll + delimiter + numberOfmsgs + delimiter;				
			}
			else{
				answer ="pathNotFound" + delimiter; 
			}
		}	
		return answer;
	}
}

//String ans = "";
//String userName = arr[0];
//int id = Integer.valueOf(arr[1]);
//String userRoll = MemberDataBase.getUserRoll(userName);
//MemberDataBase membersDataBase = MemberDataBase.getMembersDataBase();
//Member member = membersDataBase.getMember(userName);
//boolean isAdmin = member.get_isAdmin();
//if (!isAdmin)
//{
//	logger.log("not an admin try get num of msgs", Level.WARNING);
//	return "failed" + delimiter + "not an admin try get num of msgs";
//}
//else
//{
//	SubForum subForum = Forum.getForum().getSubForum(id);
//	if (subForum != null)
//	{
//		int numberOfmsgs =  subForum.getNumberOfMessagesInSubForum();
//		ans = "numOfMsgs" + delimiter + userRoll + delimiter + numberOfmsgs + delimiter;
//		
//	}
//	else
//	{
//		ans ="pathNotFound" + delimiter; 
//	}
//}
//
//return ans;
//}